//
//  YWTopicDetailTableViewController.m
//  网易新闻
//
//  Created by Yeewii@hotmail.com on 16/6/17.
//  Copyright © 2016年 Yeewii@hotmail.com. All rights reserved.
//

#import "YWTopicDetailTableViewController.h"
#import "YWTopicDetailHeaderView.h"
#import "YWHttpTool.h"
#import <MJExtension.h>
#import "YWTopicDetailModel.h"
#import "YWTopicDetailMessageCell.h"
#import <UITableView+SDAutoTableViewCellHeight.h>
#import "YWTopicPicView.h"

static NSString *const CellWithIdentifier = @"YWTopicDetailMessageCell";

@interface YWTopicDetailTableViewController ()
@property (nonatomic,strong) YWTopicDetailHeaderView *headerView;
@property (nonatomic,strong) YWTopicDetailModel *model;
@end

@implementation YWTopicDetailTableViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpViews];
    [self requestData];
}

-(void)setUpViews
{
    [self.tableView registerClass:[YWTopicDetailMessageCell class] forCellReuseIdentifier:CellWithIdentifier];
    _headerView = [[[NSBundle mainBundle]loadNibNamed:@"YWTopicDetailHeaderView" owner:nil options:nil] lastObject];
    _headerView.frame = CGRectMake(0, 0, MAINWIDTH, MAINWIDTH*420/750);
    self.tableView.tableHeaderView = _headerView;
}


-(void)requestData
{
    __weak typeof(self) weakSelf = self;
    [YWHttpTool GET:[NSString stringWithFormat:@"http://topic.comment.163.com/topic/subject/details/%@.html",_url] params:nil success:^(NSDictionary *responseObject) {
        
        _model = [YWTopicDetailModel mj_objectWithKeyValues:responseObject[@"data"]];
        
        _headerView.model = _model;
        
        [weakSelf.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self cellHeightForIndexPath:indexPath cellContentViewWidth:MAINWIDTH tableView:tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _model.hotList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    YWTopicDetailMessageCell *cell = [[YWTopicDetailMessageCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellWithIdentifier];
    YWTopicDetailHotList *hotModel = _model.hotList[indexPath.row];
    cell.model                     = hotModel;
    return cell;
}



@end
